home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / read_pict.pro < prev    next >
Text File  |  1997-07-08  |  9KB  |  299 lines

  1. ; $Id: read_pict.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1990-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. pro read_pict_item, unit, data
  7. ; procedure reverse from big-endian to little endian or vis a versa.
  8. ; On entry:
  9. ;  DATA should be defined, and items are read from unit if it is > 0.
  10. ;  If Unit is < 0, DATA is already read.
  11. ;  The common block, write_pict_rev should already be set up.
  12.  
  13. common write_pict_rev, rev
  14.  
  15. if unit ge 0 then readu, unit, data        ;Read it???
  16. if rev eq 0 then return        ;Nothing to do...
  17. s = size(data)            ;What type of data?
  18. case s[s[0]+1] of
  19. 2:    byteorder, data, /SSWAP    ;Swap shorts
  20. 3:    byteorder, data, /LSWAP ;longs
  21. 4:    byteorder, data, /LSWAP ;Float
  22. 5:    BEGIN            ;Double
  23.     n = n_elements(data)
  24.     data = byte(data, 0, n*8)
  25.     for i=0L,8*(n-1),8 do for j=0L,7 do data[i+j] = data[i+7-j]
  26.     data = double(data, 0, n)
  27.     ENDCASE
  28. 6:    byteorder, data, /LSWAP    ;Complex => floats
  29. 8:    BEGIN            ;Structure...
  30.     for i=0, n_tags(data)-1 do begin    ;Do each tag individually.
  31.        tmp = data.(i)
  32.        read_pict_item, -1, tmp
  33.        data.(i) = tmp
  34.        ENDFOR
  35.     ENDCASE
  36. else:                ;Do nothing for bytes & strings
  37. ENDCASE
  38. end    
  39.  
  40.  
  41. function UnPackData, width, height, unit
  42.  
  43. ;    Function UnPackData
  44. ;    This function is used by the READ_PICT user library routine and
  45. ;    it performs the Quickdraw style run length decoding for PICT
  46. ;    files.  IMAGE is the unpacked data.  
  47. ;    NOPROGRESS is a keyword and when set, it suppressed the display of
  48. ;    the progress of the packing.
  49.  
  50.  
  51. IMAGE = bytarr(width, height)
  52. for scanline = height-1,0, -1 do begin
  53.   scanlinecount = 0
  54.   packlineindex = 0
  55.   if(width gt 250) then count = 0 else count = 0b
  56.   read_pict_item, unit, count
  57.   packline = bytarr(count)
  58.   readu, unit, packline
  59.   while(scanlinecount lt width) do begin
  60.     packcount = packline[packlineindex]
  61.     packlineindex = packlineindex + 1
  62.     if (packcount ge 128) then begin        ; bits are packed
  63.       packcount = 256 - packcount
  64.       repeatedvalue = packline[packlineindex]
  65.       packlineindex = packlineindex + 1
  66.       IMAGE[scanlinecount:scanlinecount + packcount, scanline] = repeatedvalue
  67.     endif else begin
  68.       IMAGE[scanlinecount:scanlinecount + packcount,        $
  69.            scanline] = packline[packlineindex:packlineindex + packcount]
  70.       packlineindex = packlineindex + packcount + 1
  71.     endelse
  72.     scanlinecount = scanlinecount + packcount + 1
  73.   endwhile
  74.   if(scanlinecount ne width) then begin
  75.     print, "error in reading scanline ",scanline
  76.     stop
  77.   endif
  78. endfor
  79.  
  80. return, IMAGE
  81.  
  82. end
  83.  
  84.  
  85. PRO READ_PICT, filename, resultimage, r, g, b, DEBUG = DEBUG
  86.  
  87. ;+
  88. ; NAME:        READ_PICT
  89. ; PURPOSE:     Reads limited types of image files written in the PICT
  90. ;        Version 2 Format.  This format is used by the Apple 
  91. ;        Macintosh Computers.
  92. ; CATEGORY:    
  93. ; CALLING SEQUENCE:
  94. ;    READ_PICT, FILE, IMAGE        ;Reads PICT file into IMAGE
  95. ;    READ_PICT, FILE, IMAGE, R, G, B    ;Reads Image and loads color vectors
  96. ; INPUTS:
  97. ;       FILE = Scalar string giving the name of the PICT file to read.
  98. ;       IMAGE = 2D matrix to be input.  
  99. ; OPTIONAL INPUT PARAMETERS:
  100. ;       R, G, B = The Red, Green, and Blue color vectors to be read
  101. ;               with IMAGE.  If not specified, the color table associated
  102. ;        with the picture is ignored.
  103. ; OUTPUTS:
  104. ;    IMAGE - the image that is read in from the file.
  105. ;    R, G, B - the color vectors from the PICT file.
  106. ; SIDE EFFECTS:
  107. ;    A file is opened, UnPackData is called, I/O is performed
  108. ; RESTRICTIONS:
  109. ;    Only creates Version 2 PICT files.  Not intended to read all PICT
  110. ;    files.  Known to work with IDL PICT files written with write_pict
  111. ;    routine in IDL.
  112. ; PROCEDURE:
  113. ;    Read in the header, size, and the following quickdraw opcodes.
  114. ;
  115. ; MODIFICATION HISTORY:
  116. ;    Written 19 November 1990, Steve Richards.
  117. ;        19 November 1992, Steve Richards, Fixed a problem where
  118. ;            the color vectors returned were integer and not
  119. ;            bytes.
  120. ;        Jul 1994,  DMS, RSI.  Added code for both big and little
  121. ;            endian byte ordering.  Previous version would not
  122. ;            work on little endian machines.
  123. ;-
  124.  
  125. common write_pict_rev, rev
  126.  
  127. ON_ERROR, 2
  128. i  = byte(1,0,2)        ;Test byte ordering of this machine
  129. rev = i[0] eq 1b         ;TRUE to reverse for little endian
  130.  
  131. if keyword_set(DEBUG) then DEBUG = 1 else DEBUG = 0
  132. hdr = bytarr(512)
  133. imagesize = 0
  134. Rect = {rect, top:0, left:0, bottom:0, right:0}
  135.  
  136. openr, unit, filename, /get_lun
  137. readu, unit, hdr
  138. read_pict_item, unit, imagesize
  139. read_pict_item, unit, Rect
  140. image = bytarr(Rect.right - Rect.left, Rect.bottom - Rect.top)
  141. done = 0
  142. count = 0
  143.  
  144. while(done eq 0) do begin
  145.   opcode = 0
  146.   read_pict_item, unit, opcode
  147.   count = count + 1
  148.   
  149.   case opcode of
  150.     0: begin                    ;nop
  151.      if(DEBUG ne 0) then print, "---NOP Opcode"
  152.        end
  153.  
  154.     1: begin                    ;clip region size
  155.      if(DEBUG ne 0) then print, "---Clip Opcode"
  156.      regionsize = 0
  157.      read_pict_item, unit, regionsize
  158.      if regionsize ne 10 then begin
  159.        print, "Non rectangular regions not supported"
  160.        print, "Region is of size ",regionsize
  161.        done = 1
  162.          endif else begin
  163.        clipregion = Rect
  164.        read_pict_item, unit, clipregion
  165.            if(DEBUG ne 0) then print, "Clip Region is ",clipregion
  166.      endelse
  167.        end
  168.  
  169.     17:    begin                    ;version number
  170.       if(DEBUG ne 0) then print, "---Version Opcode"
  171.       versionnumber = 0b
  172.       lowbyte = 0b
  173.       readu, unit, versionnumber
  174.       readu, unit, lowbyte
  175.       if(DEBUG ne 0) then $
  176.         print, "  Reading PICT file with version number ",versionnumber, $
  177.            "   and lowbyte", lowbyte
  178.         end
  179.  
  180.     30: begin                    ;default highlight operation
  181.       if(DEBUG ne 0) then print, "---DefHilite Opcode"
  182.     end      
  183.  
  184.     152: begin                    ;packed copybits operation
  185.        if(DEBUG ne 0) then print, "---PackBitsRect Opcode"
  186.        pixMap = {pixMapstr,    rowBytes:0,        $
  187.                 Boundtop:0,        $
  188.                 Boundleft:0,        $
  189.                 Boundbottom:0,        $
  190.                 Boundright:0,        $
  191.                 version:0,        $
  192.                 packType:0,        $
  193.                 packSize:0L,        $
  194.                 hRes:0L,        $
  195.                 vRes:0L,        $
  196.                 pixelType:0,        $
  197.                 pixelSize:0,        $
  198.                 cmpCount:0,        $
  199.                 cmpSize:0,        $
  200.                 planeBytes:0L,        $
  201.                 pmTable:0L,        $
  202.                 pmReserved:0L}
  203.        read_pict_item, unit, pixMap
  204.        if(DEBUG ne 0) then print, "read pixMap"
  205.        pixMap.rowBytes = pixMap.rowBytes and 32767    ;strip high bit
  206.        coltable = {coltablestr,    ctseed:0L,    $
  207.                     transIndex:0,    $
  208.                     ctSize:0}
  209.        read_pict_item, unit, coltable
  210.        if(DEBUG ne 0) then help, /str, coltable
  211.        colors = intarr(4, coltable.ctSize + 1)
  212.        read_pict_item, unit, colors
  213.        if(DEBUG ne 0) then print, "read colors"
  214.        colors = byte(colors / 256)
  215.        r = reform(colors[1,*], n_elements(colors[1,*]), /overwrite)
  216.        g = reform(colors[2,*], n_elements(colors[2,*]), /overwrite)
  217.        b = reform(colors[3,*], n_elements(colors[3,*]), /overwrite)
  218.        srcRect = Rect
  219.        read_pict_item, unit, srcRect
  220.        if(DEBUG ne 0) then print, "read srcRect"
  221.        dstRect = Rect
  222.        read_pict_item, unit, dstRect
  223.        if(DEBUG ne 0) then print, "read dstRect"
  224.        mode = 0
  225.        read_pict_item, unit, mode
  226.        if(DEBUG NE 0) THEN PRINT, mode, "is the mode"
  227.        if(DEBUG ne 0) then print, "read mode"
  228.        if(pixMap.rowBytes lt 8) then begin    ;data is unpacked
  229.          if(DEBUG ne 0) then print, "Data is unpacked"
  230.          datasize = pixMap.rowBytes*(pixMap.Boundbottom -    $
  231.                      pixMap.Boundtop)
  232.          image = bytarr(pixMap.rowBytes*                $
  233.                 (pixMap.Boundbottom - pixMap.BoundTop))
  234.          readu, unit, image
  235.        endif else begin            ;data is packed
  236.          if(DEBUG ne 0) then print, "Data is packed"
  237.          image = UnPackData(pixMap.rowBytes,            $
  238.                 pixMap.Boundbottom - pixMap.BoundTop, unit)
  239.        endelse
  240.        done = 1
  241.      end
  242.  
  243.     154: begin        ;reserved apple instruction
  244.         if(DEBUG ne 0) then print, "---Reserved Apple Opcode 0x009A"
  245.            datasize = 0
  246.        read_pict_item, unit, datasize
  247.            if(datasize ne 0) then begin
  248.          trash = bytarr(datasize)
  249.          readu, unit, trash
  250.          if(DEBUG ne 0) then print, "  datasize ",datasize, "      data", $
  251.                     trash
  252.        endif else begin
  253.          if(DEBUG ne 0) then print, "  datasize = 0"
  254.        endelse
  255.      end
  256.  
  257.     255: begin                    ;end of pict file opcode
  258.        if(DEBUG ne 0) then begin
  259.          print, "---opEndPic Opcode"
  260.           stuff = bytarr(16)
  261.          readu, unit, stuff
  262.          print, stuff, format = '("  Rest in HEX ",16(" ",Z2.2," "))
  263.          print, stuff, format = '("  Rest in DEC ",16(I3.3," "))
  264.        endif       
  265.        done = 1
  266.      end
  267.  
  268.     3072: begin                    ;header operation
  269.         headerdata = bytarr(24)
  270.         readu, unit, headerdata
  271.         if(DEBUG ne 0) then print, "---HeaderOp Opcode"
  272.         if(DEBUG ne 0) then $
  273.           print, headerdata, $
  274.              format = '("  Header data is ",3(/"    ",8(Z2.2," ")))'
  275.       end
  276.  
  277.     else: begin                    ;unknown opcode
  278.         print, opcode, opcode, $
  279.            format = '("Stopped on unknown opcode $",Z4.4," or ",I)'
  280.         if(DEBUG ne 0) then begin 
  281.           print, "Dump follows"
  282.           opcodes = bytarr(10)
  283.               readu, unit, opcodes
  284.           print, opcodes, format = '(10(I3,"  "))'
  285.         endif
  286.         done = 1
  287.       end
  288.   endcase
  289.  
  290. endwhile
  291.  
  292. free_lun, unit
  293. resultimage = image
  294.  
  295. end
  296.  
  297.  
  298.  
  299.